﻿using System;
using Fusion.States;
using Fusion.Management;
using Microsoft.Xna.Framework.Input;
using Lidgren.Network;
using Microsoft.Xna.Framework;
using Fusion.Content;
using Fusion.Interface;
using EclipseLib.Shared;
using Client.Game_States.Game_GUI_s;
using RamGecXNAControls;
using RamGecXNAControls.ExtendedControls;

namespace Client.Game_States
{
    public class MainMenu : BaseState
    {
        // Constructor
        public MainMenu() { }

        // Data Members
        ClientSocket.ReceiveData NetworkDataFunc;
        GameTexture Logo, Background;
        MainMenuGUI GUI;

        GUIControl.ClickEventHandler LoginButtonClicked, ExitButtonClicked, CreditsButtonClicked, ConnectButtonClicked, RegisterButtonClicked;

        // State Methods
        public override void Enter()
        {
            NetworkDataFunc = new ClientSocket.ReceiveData(IncomingPacket);
            Network.Socket.OnReceiveData += NetworkDataFunc;
            FCanvas.ClearColor = Color.Black;

            Logo = GFXHelper.LoadInterfaceTexture("Logo");
            Background = GFXHelper.LoadInterfaceTexture("Background");

            GUI = new MainMenuGUI();
            LoginButtonClicked = new GUIControl.ClickEventHandler(LoginClick);
            ExitButtonClicked = new GUIControl.ClickEventHandler(ExitClick);
            CreditsButtonClicked = new GUIControl.ClickEventHandler(CreditsClick);
            ConnectButtonClicked = new GUIControl.ClickEventHandler(ConnectClick);
            RegisterButtonClicked = new GUIControl.ClickEventHandler(RegisterClick);

            GUI.LoginButton.OnClick += LoginButtonClicked;
            GUI.ExitButton.OnClick += ExitButtonClicked;
            GUI.CreditsButton.OnClick += CreditsButtonClicked;
            GUI.ConnectButton.OnClick += ConnectButtonClicked;
            GUI.RegisterButton.OnClick += RegisterButtonClicked;

            ConnectionChanged(Network.IsConnected());
        }
        public override void Exit()
        {
            Network.Socket.OnReceiveData -= NetworkDataFunc;

            Logo.Release();
            Background.Release();

            GUI.LoginButton.OnClick -= LoginButtonClicked;
            GUI.ExitButton.OnClick -= ExitButtonClicked;

            LoginButtonClicked = null;
            ExitButtonClicked = null;

            GUI.Dispose();
        }
        public override bool Input(float Delta)
        {
            return true;
        }
        public override void Update(float Delta)
        {
            GUI.Update();
            //ConnectionChanged(Network.IsConnected());
        }
        public override void Render(float Delta)
        {
            FCanvas.Begin();
            {
                FCanvas.Draw(Background, Vector2.Zero, Color.White);

                FCanvas.Draw(Logo, new Vector2(GUI.LoginWindow.Bounds.Location.X + 17, 150f), Color.Black);
                FCanvas.Draw(Logo, new Vector2(GUI.LoginWindow.Bounds.Location.X + 15, 150f), Color.Black);
                FCanvas.Draw(Logo, new Vector2(GUI.LoginWindow.Bounds.Location.X + 16, 151f), Color.Black);
                FCanvas.Draw(Logo, new Vector2(GUI.LoginWindow.Bounds.Location.X + 16, 149f), Color.Black);
                FCanvas.Draw(Logo, new Vector2(GUI.LoginWindow.Bounds.Location.X + 16, 150f), Color.White);

                // Controls.Render(Delta);
                GUI.Render();
            }
            FCanvas.End();
        }

        // Network Methods
        private void IncomingPacket(NetIncomingMessage Msg)
        {
            switch (Msg.MessageType)
            {
                case NetIncomingMessageType.Data:
                    {
                        ServerPackets Packet = (ServerPackets)Msg.ReadInt32();

                        switch (Packet)
                        {
                            case ServerPackets.SendErrorMessage:
                                {
                                    GUI.ShowError(Msg.ReadString());
                                }
                                break;

                            case ServerPackets.SendShutDown:
                                ConnectionChanged(false);
                                break;

                            case ServerPackets.SendLoginState:
                                {
                                    LoginState State = (LoginState)Msg.ReadInt32();

                                    switch (State)
                                    {
                                        case LoginState.NewCharacter:
                                            FStates.ChangeState(new NewCharacter(), false);
                                            break;

                                        case LoginState.PlayGame:
                                            break;
                                    }
                                }
                                break;
                        }
                    }
                    break;
            }
        }
        private void ConnectionChanged(bool Connected)
        {
            if (Connected)
            {
                GUI.ServerLabel.Text = "Connected";
                GUI.ServerLabel.TextColor = Color.Green;
            }
            else
            {
                GUI.ServerLabel.Text = "Not Connected";
                GUI.ServerLabel.TextColor = Color.Red;
            }
        }

        // Control Methods
        public void LoginClick(GUIControl Sender)
        {
            if (Network.IsConnected())
            {
                NetOutgoingMessage Packet = Network.CreatePacket();
                Packet.Write((Int32)ClientPackets.SendLogIn);
                Packet.Write(GUI.UserBox.Text);
                Packet.Write(GUI.PassBox.Text);
                Network.SendData(Packet);
            }
            else
                GUI.ShowError("Not connected, press the connect button to connect.");
        }
        public void ExitClick(GUIControl Sender)
        {
            FStates.ChangeState(null, false);
        }
        public void CreditsClick(GUIControl Sender)
        {
            FStates.ChangeState(new MainMenu(), false);
        }
        public void ConnectClick(GUIControl Sender)
        {
            Network.DiscoverServer();
            ConnectionChanged(Network.IsConnected());
        }
        public void RegisterClick(GUIControl Sender)
        {
            FStates.ChangeState(new RegisterMenu(), false);
        }
    }
}
